//
//  MM_HomeSearchHeaderView.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/11/1.
//

#import "MM_HomeSearchDataView.h"
#import "MM_HomeSearchLeftCell.h"
#import "MM_HomeSearchRightCell.h"

@interface MM_HomeSearchDataView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *leftTableView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UITableView *rightTableView;

@end

@implementation MM_HomeSearchDataView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        _leftTableView = [UITableView estimatedLayoutTableview:CGRectZero tableviewStyle:UITableViewStyleGrouped delegateAndDataSource:self estimatedSectionHeader:YES estimatedSectionFooter:YES];
        _leftTableView.backgroundColor = [UIColor whiteColor];
        _leftTableView.showsVerticalScrollIndicator = NO;
        _leftTableView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_leftTableView];
        
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = MM_ColorUtil(@"#333333");
        [self addSubview:_lineView];
        
        _rightTableView = [UITableView estimatedLayoutTableview:CGRectZero tableviewStyle:UITableViewStyleGrouped delegateAndDataSource:self estimatedSectionHeader:YES estimatedSectionFooter:YES];
        _rightTableView.backgroundColor = [UIColor whiteColor];
        _rightTableView.showsVerticalScrollIndicator = NO;
        _rightTableView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_rightTableView];
        
        [self setUpViews];
    }
    return self;
}

- (void)setUpViews {
    
    [self.leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self);
        make.top.equalTo(self);
        make.width.equalTo(180);
        make.height.equalTo(self.frame.size.height);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.leftTableView.mas_right);
        make.top.equalTo(self.leftTableView);
        make.width.equalTo(2);
        make.height.equalTo(self.leftTableView);
        
    }];
    
    [self.rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.lineView.mas_right);
        make.top.equalTo(self.leftTableView);
        make.width.equalTo(ScreenW - 180);
        make.height.equalTo(self.leftTableView);
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == self.leftTableView) {
        return self.leftArr.count;
    }
    return self.rightArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.leftTableView) {
        
        MM_HomeSearchLeftCell *cell = [MM_HomeSearchLeftCell cellWithTableView:tableView];
        cell.model = self.leftArr[indexPath.row];
        return cell;
    }else {
        MM_HomeSearchRightCell *cell = [MM_HomeSearchRightCell cellWithTableView:tableView];
        cell.model = self.rightArr[indexPath.row];
        return cell;
    }
    return nil;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (tableView == self.leftTableView) {
                
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 40)];
        
        UILabel *nameLabel = [Custom_ZHTool uiLabelCreate:@"库区" Font:FONT(13) TextColor:[ColorUtil colorWithHexadecimal:@"#333333"]];
        nameLabel.font = FONT_BOLD(12);
        nameLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:nameLabel];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.centerX.centerY.equalTo(headerView);
        }];
        
        return headerView;

    }else {
        
        NSArray *titleArr = @[@"库位编码",@"货架号",@"层号",@"位号"];
        CGFloat titleW = (ScreenW - 180) /4.0;
        
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 40)];
        
        for (UIView*subView in headerView.subviews) {
            [subView removeFromSuperview];
        }
        
        for (int i=0; i<titleArr.count; i++) {
            
            UIView *titleView = [[UIView alloc] init];
            titleView.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
            [headerView addSubview:titleView];
            
            [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.left.equalTo(titleW *i);
                make.top.height.equalTo(headerView);
                make.width.equalTo(titleW);
            }];
                        
            UILabel *nameLabel = [Custom_ZHTool uiLabelCreate:titleArr[i] Font:FONT(13) TextColor:[ColorUtil colorWithHexadecimal:@"#333333"]];
            nameLabel.text = titleArr[i];
            nameLabel.font = FONT_BOLD(12);
            [titleView addSubview:nameLabel];
            
            [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.centerX.centerY.equalTo(titleView);
            }];
        }
        return headerView;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (tableView == self.leftTableView) {
        
        return 40;
    }else {
        return 40;
    }
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView == self.leftTableView) {
        
        MM_HomeSearchLeftModel *model = self.leftArr[indexPath.row];
        
        for (MM_HomeSearchLeftModel *model in self.leftArr) {
            model.is_Selected = NO;
        }
        model.is_Selected = YES;
        [self libraryGoodsData:model];
        
        [self.leftTableView reloadData];
        
    } else {
        
        MM_HomeSearchRightModel *model = self.rightArr[indexPath.row];
        
        if (self.searchBlock) {
            self.searchBlock(model);
        }
    }
}

- (void)setLeftArr:(NSArray<MM_HomeSearchLeftModel *> *)leftArr {
    _leftArr = leftArr;
    
    [self.leftTableView reloadData];
}

- (void)setRightArr:(NSArray<MM_HomeSearchRightModel *> *)rightArr {
    _rightArr = rightArr;
    
    [self.rightTableView reloadData];
}

- (void)libraryGoodsData:(MM_HomeSearchLeftModel *)model {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    NSMutableDictionary *locationNameParmDic = [NSMutableDictionary dictionary];
    NSMutableArray *tempArr = [NSMutableArray array];
    locationNameParmDic[@"name"] = @"location_name";
    locationNameParmDic[@"operator"] = @"6";
    locationNameParmDic[@"text"] = model.area_name;
    locationNameParmDic[@"value"] = model.area_name;
    [tempArr addObject:locationNameParmDic];
    
    NSMutableDictionary *warehouse_nameDic = [NSMutableDictionary dictionary];
    warehouse_nameDic[@"name"] = @"warehouse_name";
    warehouse_nameDic[@"operator"] = @"6";
    warehouse_nameDic[@"text"] = model.warehouse_name;
    warehouse_nameDic[@"value"] = model.warehouse_name;
    [tempArr addObject:warehouse_nameDic];
    
    parmDic[@"searchObjects"] = tempArr;
    
    [MM_Http postRequestURLStr:KMM_Goodslocation_List withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        
        NSArray *dataArr = [MM_HomeSearchRightModel mj_objectArrayWithKeyValuesArray:requestDic[@"rows"]];
        self.rightArr = dataArr;
        [self.rightTableView reloadData];
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
    }];
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    if (self.scroBlock) {
        self.scroBlock();
    }
}

@end
